﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data;
using System.Data.SqlClient;

namespace DAO
{
    public class KhachHangDAO
    {
        public static List<KhachHang> Search(String str)
        {
            List<KhachHang> list = new List<KhachHang>();
            String strSql = "select * from KhachHang where TenKhachHang like N'%" + str + "%'";

            try
            {
                DataTable dt = SqlDataAccessHelper.ExcuteQuery(strSql);
                foreach (DataRow dr in dt.Rows)
                {
                    KhachHang emp = new KhachHang();
                    emp.MaKhachHang = dr["MaKhachHang"].ToString();
                    emp.TenKhachHang = dr["TenKhachHang"].ToString();
                    emp.NgaySinh = dr["NgaySinh"].ToString();
                    emp.CMND = dr["CMND"].ToString();
                    emp.DiaChi = dr["DiaChi"].ToString();
                    emp.DienThoai = dr["DienThoai"].ToString();
                    list.Add(emp);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return list;
        }

        public static List<KhachHang> Search(KhachHang emp1)
        {
            List<KhachHang> list = new List<KhachHang>();
            String strSql = "select * from KhachHang where NgaySinh='"+emp1.NgaySinh+"' and GioiTinh="+emp1.GioiTinh+" ";
            if (!emp1.TenKhachHang.Equals(""))
                strSql += "and TenKhachHang like N'%" + emp1.TenKhachHang + "%' ";
            if (!emp1.CMND.Equals(""))
                strSql += "and CMND='" + emp1.CMND + "' ";
            if (!emp1.DiaChi.Equals(""))
                strSql += "and DiaChi='" + emp1.DiaChi + "' ";
            if (!emp1.DienThoai.Equals(""))
                strSql += "and DienThoai='" + emp1.DienThoai + "' ";

            try
            {
                DataTable dt = SqlDataAccessHelper.ExcuteQuery(strSql);
                foreach (DataRow dr in dt.Rows)
                {
                    KhachHang emp = new KhachHang();
                    emp.MaKhachHang = dr["MaKhachHang"].ToString();
                    emp.TenKhachHang = dr["TenKhachHang"].ToString();
                    emp.NgaySinh = dr["NgaySinh"].ToString();
                    emp.CMND = dr["CMND"].ToString();
                    emp.DiaChi = dr["DiaChi"].ToString();
                    emp.DienThoai = dr["DienThoai"].ToString();
                    if (dr["GioiTinh"].ToString().Equals("1"))
                        emp.GioiTinh = "Nam";
                    else
                        emp.GioiTinh = "Nữ";
                    list.Add(emp);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return list;
        }
    }
}
